<?php

namespace Home\Controller;


use Think\Controller;
use Think\Model;

class ApiController extends Controller {
    function __construct() {
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Headers: *");
        header('Access-Control-Allow-Methods:*');
        $methodName = strtolower($_SERVER['REQUEST_METHOD']);

        if ($methodName == "options") {
            exit;
        }
    }
    /*
     * 获取地区
     */
    public function getRegion() {
        $parent_id = I('get.parent_id');
        $selected = I('get.selected', 0);
        $data = M('region')->where("parent_id=$parent_id")->select();
        $html = '';
        if ($data) {
            foreach ($data as $h) {
                if ($h['id'] == $selected) {
                    $html .= "<option value='{$h['id']}' selected>{$h['name']}</option>";
                }
                $html .= "<option value='{$h['id']}'>{$h['name']}</option>";
            }
        }
        echo $html;
    }


    public function getTwon() {
        $parent_id = I('get.parent_id');
        $data = M('region')->where("parent_id=$parent_id")->select();
        $html = '';
        if ($data) {
            foreach ($data as $h) {
                $html .= "<option value='{$h['id']}'>{$h['name']}</option>";
            }
        }
        if (empty($html)) {
            echo '0';
        } else {
            echo $html;
        }
    }

    /*
     * 获取商品分类
     */
    public function get_category() {
        $parent_id = I('get.parent_id', '0'); // 商品分类 父id  
        empty($parent_id) && exit('');
        $list = M('goods_category')->where(array('parent_id' => $parent_id))->select();
        foreach ($list as $k => $v) {
            $html .= "<option value='{$v['id']}' rel='{$v['commission']}'>{$v['name']}</option>";
        }
        exit($html);
    }
    /*
     * 获取商品分类
     */
    public function get_category_store() {
        $parent_id = I('get.parent_id', '0'); // 商品分类 父id  
        empty($parent_id) && exit('');
        $list = M('store_goods_class')->where(array('parent_id' => $parent_id))->select();
        foreach ($list as $k => $v) {
            $html .= "<option value='{$v['cat_id']}' rel='{$v['commission']}'>{$v['cat_name']}</option>";
        }
        exit($html);
    }

    public function get_cates() {
        $parent_id = I('get.parent_id', '0'); // 商品分类 父id
        empty($parent_id) && exit('');
        $list = M('goods_category')->where(array('parent_id' => $parent_id))->select();
        foreach ($list as $k => $v) {
            $html .= "<input type='checkbox' name='subcate[]' rel='{$v['commission']}' data-name='{$v['name']}' value='{$v['id']}'>" . $v['name'];
        }
        exit($html);
    }
    /*
     * 获取店铺内分类
     */
    public function get_store_category() {
        // 店铺id
        $store_id = session('store_id');
        $store_id = $store_id ? $store_id : 0;
        $parent_id = I('get.parent_id', 0); // 商品分类 父id  

        ($parent_id == 0) && exit('');

        $list = M('store_goods_class')->where("parent_id = $parent_id and store_id = $store_id")->select();
        foreach ($list as $k => $v)
            $html .= "<option value='{$v['cat_id']}'>{$v['cat_name']}</option>";
        exit($html);
    }

    /**
     * 检测手机号是否已经存在
     */
    public function issetMobile() {
        $mobile = I("mobile", '0');
        $users = M('users')->where("mobile = '$mobile'")->find();
        if ($users)
            exit('1');
        else
            exit('0');
    }
    public function panelData() {
        $begin = strtotime(date('Y-m-d', time()) . ' 00:00:00');
        $end = strtotime(date('Y-m-d', time()) . ' 23:59:59');
        $condition['add_time'] = array('between', "$begin,$end");
        $condition['pay_code'] = 'cod';
        // $panel_data = F('gd_panel_data');
        // if(!$panel_data){
        $panel_data = gx_update_panel_data();
        // }
        dump($panel_data);

        // $model = new Model();
        // $csj_db = $model->db('2',"mysql://tlcgp:AWzrhCa8fJ@139.9.50.10:3306/csj_shop");
        // $csj_order_count = $csj_db->table('ty_order')->where($condition)->count();
        // dump($order_count);
        // dump($csj_order_count);
    }


    public function panelStoreLists() {
        $page = $_POST['page'] ?? 1;

        $begin = strtotime(date('Y-m-d', time()) . ' 00:00:00');
        $end = strtotime(date('Y-m-d', strtotime('+1 days', time())) . ' 00:00:00');
        $condition['add_time'] = array('between', "$begin,$end");
        $condition['order_status'] = array('in', [0, 1, 2]);

        $fenbu = M('fenbu')->field('fenbu_id')->select();
        $fenbu_id = array_column($fenbu, 'fenbu_id');
        $condition['ufenbu_id'] = array('in', $fenbu_id);
        $count = M('order a')
            ->field('b.store_name,sum(a.order_amount) as order_price_count,count(a.order_id) as order_count')
            ->join('ty_store b on b.store_id=a.store_id')
            ->where($condition)
            ->group('b.store_id')->select();

        $page_start = $page - 1;

        //图表3 店铺排名
        $store_order_list = M('order a')
            ->field('b.store_name,sum(a.order_amount) as order_price_count,count(a.order_id) as order_count')
            ->join('ty_store b on b.store_id=a.store_id')
            ->where($condition)
            ->group('b.store_id')->order('order_price_count desc')->limit("{$page_start},30")->select();

        echo json_encode(['count' => count($count), 'data' => $store_order_list]);
    }
    public function panelXianLists() {
        $page = $_POST['page'] ?? 1;
        $begin = strtotime(date('Y-m-d', time()) . ' 00:00:00');
        $end = strtotime(date('Y-m-d', strtotime('+1 days', time())) . ' 00:00:00');

        //图表2 线路排名
        $condition_xian['chuku_last_time'] = array('between', "$begin,$end");
        $condition_xian['order_status'] = array('in', [0, 1, 2, 4]);
        $count = M('order a')
            ->field('b.biaoshi_xian,sum(a.order_amount+a.yijia_heji) as order_price_count,count(a.user_id) as user_count')
            ->join('ty_users b on b.user_id=a.user_id')
            ->where($condition_xian)
            ->group('b.biaoshi_xian')->select();

        $page_start = $page - 1;

        //图表3 店铺排名
        $xian_order_list = M('order a')
            ->field('b.biaoshi_xian,sum(a.order_amount+a.yijia_heji) as order_price_count,count(a.user_id) as user_count')
            ->join('ty_users b on b.user_id=a.user_id')
            ->where($condition_xian)
            ->group('b.biaoshi_xian')->order('order_price_count desc')->limit("{$page_start},30")->select();

        echo json_encode(['count' => count($count), 'data' => $xian_order_list]);
    }
    public function panelGoodsLists() {
        $page = $_POST['page'] ?? 1;

        $begin = strtotime(date('Y-m-d', time()) . ' 00:00:00');
        $end = strtotime(date('Y-m-d', strtotime('+1 days', time())) . ' 00:00:00');
        $condition['add_time'] = array('between', "$begin,$end");
        $condition['order_status'] = array('in', [0, 1, 2]);

        $fenbu = M('fenbu')->field('fenbu_id')->select();
        $fenbu_id = array_column($fenbu, 'fenbu_id');
        $condition['ufenbu_id'] = array('in', $fenbu_id);
        $count = M('order a')
            ->field('c.goods_name,sum(a.order_amount) as order_price_count,count(a.order_id) as order_count')
            ->join('ty_order_goods b on b.order_id=a.order_id')
            ->join('ty_goods c on c.goods_id=b.goods_id')
            ->where($condition)
            ->where(['c.prom_id' => ['neq', 0]])
            ->group('b.goods_id')->select();

        $page_start = $page - 1;

        //图标4 活动产品销量

        $goods_order_list = M('order a')
            ->field('c.goods_name,sum(a.order_amount) as order_price_count,count(a.order_id) as order_count')
            ->join('ty_order_goods b on b.order_id=a.order_id')
            ->join('ty_goods c on c.goods_id=b.goods_id')
            ->where($condition)
            ->where(['c.prom_id' => ['neq', 0]])
            ->group('b.goods_id')->order('order_price_count desc')->limit("{$page_start},30")->select();

        echo json_encode(['count' => count($count), 'data' => $goods_order_list]);
    }
    public function panelGxStoreLists() {
        $page = $_POST['page'] ?? 1;

        $begin = strtotime(date('Y-m-d', time()) . ' 00:00:00');
        $end = strtotime(date('Y-m-d', strtotime('+1 days', time())) . ' 00:00:00');
        $condition['add_time'] = array('between', "$begin,$end");
        $condition['order_status'] = array('in', [0, 1, 2]);
        $model = new Model();
        $csj_db = $model->db('2', "mysql://tlcgp:AWzrhCa8fJ@139.9.50.10:3306/csj_shop");

        $fenbu = $csj_db->table('ty_fenbu')->field('fenbu_id')->select();
        $fenbu_id = array_column($fenbu, 'fenbu_id');
        $condition['ufenbu_id'] = array('in', $fenbu_id);

        $count = $csj_db->table('ty_order a')
            ->field('b.store_name,sum(a.order_amount) as order_price_count,count(a.order_id) as order_count')
            ->join('ty_store b on b.store_id=a.store_id')
            ->where($condition)
            ->group('b.store_id')->select();

        $page_start = $page - 1;

        //图表3 店铺排名
        $store_order_list = $csj_db->table('ty_order a')
            ->field('b.store_name,sum(a.order_amount) as order_price_count,count(a.order_id) as order_count')
            ->join('ty_store b on b.store_id=a.store_id')
            ->where($condition)
            ->group('b.store_id')->order('order_price_count desc')->limit("{$page_start},30")->select();


        echo json_encode(['count' => count($count), 'data' => $store_order_list]);
    }
    public function panelGxXianLists() {
        $page = $_POST['page'] ?? 1;
        $begin = strtotime(date('Y-m-d', time()) . ' 00:00:00');
        $end = strtotime(date('Y-m-d', strtotime('+1 days', time())) . ' 00:00:00');


        $model = new Model();
        $csj_db = $model->db('2', "mysql://tlcgp:AWzrhCa8fJ@139.9.50.10:3306/csj_shop");
        //图表2 线路排名
        $condition_xian['chuku_last_time'] = array('between', "$begin,$end");
        $condition_xian['order_status'] = array('in', [0, 1, 2, 4]);
        $count = $csj_db->table('ty_order a')
            ->field('b.biaoshi_xian,sum(a.order_amount+a.yijia_heji) as order_price_count,count(a.user_id) as user_count')
            ->join('ty_users b on b.user_id=a.user_id')
            ->where($condition_xian)
            ->group('b.biaoshi_xian')->select();

        $page_start = $page - 1;

        //图表3 店铺排名
        $xian_order_list = $csj_db->table('ty_order a')
            ->field('b.biaoshi_xian,sum(a.order_amount+a.yijia_heji) as order_price_count,count(a.user_id) as user_count')
            ->join('ty_users b on b.user_id=a.user_id')
            ->where($condition_xian)
            ->group('b.biaoshi_xian')->order('order_price_count desc')->limit("{$page_start},30")->select();

        echo json_encode(['count' => count($count), 'data' => $xian_order_list]);
    }

    public function panelGxGoodsLists() {
        $page = $_POST['page'] ?? 1;

        $begin = strtotime(date('Y-m-d', time()) . ' 00:00:00');
        $end = strtotime(date('Y-m-d', strtotime('+1 days', time())) . ' 00:00:00');
        $condition['add_time'] = array('between', "$begin,$end");
        $condition['order_status'] = array('in', [0, 1, 2]);
        $model = new Model();
        $csj_db = $model->db('2', "mysql://tlcgp:AWzrhCa8fJ@139.9.50.10:3306/csj_shop");

        $fenbu = $csj_db->table('ty_fenbu')->field('fenbu_id')->select();
        $fenbu_id = array_column($fenbu, 'fenbu_id');
        $condition['ufenbu_id'] = array('in', $fenbu_id);

        $count = $csj_db->table('ty_order a')
            ->field('c.goods_name,sum(a.order_amount) as order_price_count,count(a.order_id) as order_count')
            ->join('ty_order_goods b on b.order_id=a.order_id')
            ->join('ty_goods c on c.goods_id=b.goods_id')
            ->where($condition)
            ->where(['c.prom_id' => ['neq', 0]])
            ->group('b.goods_id')->select();

        $page_start = $page - 1;

        //图标4 活动产品销量

        $goods_order_list = $csj_db->table('ty_order a')
            ->field('c.goods_name,sum(a.order_amount) as order_price_count,count(a.order_id) as order_count')
            ->join('ty_order_goods b on b.order_id=a.order_id')
            ->join('ty_goods c on c.goods_id=b.goods_id')
            ->where($condition)
            ->where(['c.prom_id' => ['neq', 0]])
            ->group('b.goods_id')->order('order_price_count desc')->limit("{$page_start},30")->select();

        echo json_encode(['count' => count($count), 'data' => $goods_order_list]);
    }
}
